package cyan.gf.util;

/**
 * Created by cyan on 2017/1/23.
 * Email:qsyan@vip.qq.com
 */
public class NameUtil {

  public static String asClassName(String name) {
    String[] sname = name.split("_");
    if (sname.length == 1) {
      return String.valueOf(name.charAt(0)).toUpperCase() + name.substring(1, name.length()).toLowerCase();
    }
    StringBuilder stringBuilder = new StringBuilder();
    for (String s : sname) {
      String fString = String.valueOf(s.charAt(0)).toUpperCase();
      String lString = s.substring(1, s.length()).toLowerCase();
      stringBuilder.append(fString + lString);
    }
    return stringBuilder.toString();
  }

  public static String asFieldName(String name) {
    String[] sname = name.split("_");
    if (sname.length == 1) {
      if (Character.isLowerCase(name.charAt(0))) {
        return name;
      } else {
        return String.valueOf(name.charAt(0)).toLowerCase() + name.substring(1, name.length());
      }
    } else {
      StringBuilder stringBuilder = new StringBuilder();
      int flag = 0;
      for (String s : sname) {
        flag += 1;
        String fString;
        if (flag >= 2) {
          fString = String.valueOf(s.charAt(0)).toUpperCase();
        } else {
          fString = String.valueOf(s.charAt(0)).toLowerCase();
        }
        String lString = s.substring(1, s.length()).toLowerCase();
        stringBuilder.append(fString + lString);
      }
      return stringBuilder.toString();
    }
  }

}
